import { CaseEntity, CaseStatus } from '../entity/CaseEntity';
import { SuspectEntity } from '../entity/SuspectEntity';
import { ClueEntity } from '../entity/ClueEntity';
import { EvidenceValue } from '../value/EvidenceValue';

// 案件分析领域服务
export class CaseAnalysisService {
  // 分析嫌疑人的嫌疑程度
  public analyzeSuspicionLevel(
    suspect: SuspectEntity,
    clues: ClueEntity[],
    evidence: EvidenceValue[]
  ): number {
    let suspicionScore = suspect.suspicionLevel;
    
    // 根据相关线索增加嫌疑分数
    const relatedClues = clues.filter(clue => 
      clue.relatedSuspects.includes(suspect.id)
    );
    suspicionScore += relatedClues.reduce((sum, clue) => sum + clue.importance, 0);
    
    // 根据证据调整嫌疑分数
    const suspectEvidence = evidence.filter(ev => 
      ev.description.includes(suspect.name) || 
      ev.analysisReport.includes(suspect.name)
    );
    suspicionScore += suspectEvidence.filter(ev => ev.isValid).length * 2;
    
    // 确保分数在1-5范围内
    return Math.min(5, Math.max(1, Math.round(suspicionScore / 3)));
  }
  
  // 判断案件是否可以结案
  public canCloseCase(caseEntity: CaseEntity, suspect: SuspectEntity): boolean {
    if (caseEntity.status !== CaseStatus.SOLVED) {
      return false;
    }
    
    // 必须有一个确定的犯罪嫌疑人
    return suspect.isGuilty;
  }
  
  // 计算案件的解决进度
  public calculateCaseProgress(
    caseEntity: CaseEntity,
    interviewedSuspects: number,
    analyzedClues: number,
    analyzedEvidence: number
  ): number {
    const totalSuspects = caseEntity.suspects.length || 1;
    const totalClues = caseEntity.clues.length || 1;
    const totalEvidence = caseEntity.evidence.length || 1;
    
    const suspectProgress = interviewedSuspects / totalSuspects;
    const clueProgress = analyzedClues / totalClues;
    const evidenceProgress = analyzedEvidence / totalEvidence;
    
    // 加权计算总进度
    return Math.round((suspectProgress * 0.4 + clueProgress * 0.3 + evidenceProgress * 0.3) * 100);
  }
}
